package com.csweimei.rentmanagementtreasure.pop;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.CustomerDetailBean;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.customer.AddCustomerActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.customer.AddRecordActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.customer.AddZyActivity;
import com.csweimei.rentmanagementtreasure.utils.FileUtils;
import com.csweimei.rentmanagementtreasure.utils.permission.RxPermissionsUtils;
import com.lxj.xpopup.core.BottomPopupView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import me.goldze.mvvmhabit.utils.ToastUtils;

public class PopEditCustomer extends BottomPopupView {
    private Context mContext;


    private LinearLayout share,add_zy,add_gj,del,edtCostomer;


    public PopEditCustomer(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    public PopSelClick mPopSelClick;
    public void setSelBack(PopSelClick popSelClick){
        this.mPopSelClick = popSelClick;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_edit_customer;
    }

    @Override
    protected void onCreate() {
        share = findViewById(R.id.share);
        add_zy = findViewById(R.id.add_zy);
        add_gj = findViewById(R.id.add_gj);
        del = findViewById(R.id.del);
        edtCostomer = findViewById(R.id.edit_costomer);


        ImageView cancel = findViewById(R.id.pop_close);
        cancel.setOnClickListener(view -> dismiss());

        add_zy.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, AddZyActivity.class);
            intent.putExtra(AppConfig.BUNDLE_KEY,mData);
            mContext.startActivity(intent);
            dismiss();
        });

        add_gj.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, AddRecordActivity.class);
            intent.putExtra("id",mId);
            mContext.startActivity(intent);
            dismiss();
        });

        del.setOnClickListener(v -> {
            mPopSelClick.sel(new PopBean("DEL"));
            dismiss();
        });

        edtCostomer.setOnClickListener(v -> {
            dismiss();
            Intent intent = new Intent(mContext, AddCustomerActivity.class);
            intent.putExtra("id",mId);
            intent.putExtra("flag","edit");
            mContext.startActivity(intent);

        });

        share.setOnClickListener(v -> {
            RxPermissionsUtils.fileAll((Activity) mContext, new RxPermissionsUtils.Accept() {
                @Override
                public void accept() {
                    if (viewGroup != null){

                        saveImageToGallery(mContext,getBitmapFromView(viewGroup));

                    }
                }
            });
        });
    }



    public Bitmap getBitmapFromView(View view) {
        // 创建和布局相同大小的Bitmap
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        // 将布局绘制在Bitmap上
        view.draw(canvas);
        return bitmap;
    }


    private boolean saveImageToGallery(Context mContext, Bitmap bmp) {
        // 首先保存图片
        String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "zgb";
        String fileName = "zgb_custom.jpg";

        FileUtils.createNewDir(storePath);
//        File appDir = new File(storePath);
//        if (!appDir.exists()) {
//            appDir.mkdir();
//        }

        File file = new File(storePath, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            //通过io流的方式来压缩保存图片
            boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos);
            fos.flush();
            fos.close();

            //把文件插入到系统图库
            //MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);

            //保存图片后发送广播通知更新数据库
            Uri uri = Uri.fromFile(file);
            mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
            if (isSuccess) {
                ToastUtils.showShort("保存成功");

                Uri imageUri = Uri.parse(MediaStore.Images.Media.insertImage(mContext.getContentResolver(),
                        bmp, null, null));
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_SEND);
                intent.putExtra(Intent.EXTRA_STREAM, imageUri);
                intent.setType("image/*");
                mContext.startActivity(Intent.createChooser(intent, "分享到"));

                viewGroup.destroyDrawingCache();
                viewGroup.setDrawingCacheEnabled(false);
                dismiss();
                return true;
            } else {
                ToastUtils.showShort("保存失败");
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public String mId = "0";

    private CustomerDetailBean.DataDTO mData;
    public void setSelId(String id) {
        mId = id;
    }

    public void setCustomerBean(CustomerDetailBean.DataDTO dataDTO) {
        mData = dataDTO;
    }


    ViewGroup viewGroup;
    public void setShareView(ViewGroup view){
        viewGroup = view;
    }
}
